package com.jeequan.jeepay.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;

import javax.net.ssl.SSLContext;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class DisResultHttp {


	public static CloseableHttpClient trustAll(){
		//配置，发送https请求时，忽略ssl证书认证（否则会报错没有证书）
		SSLContext sslContext = null;
		try {
			sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
				@Override
				public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
					return true;
				}
			}).build();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}

//创建httpClient
		CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build();


		return client;
	}

	public static DistResult doPostHttpsAir(String host, Object params, String token, String userName) throws URISyntaxException {
		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectTimeout(3000)
				.setConnectionRequestTimeout(3000)
				.setSocketTimeout(3000)
				.build();

		HttpPost post = new HttpPost(host);
		post.setConfig(requestConfig);

		try {
			// 验证 params 不为 null
			if (params == null) {
				throw new IllegalArgumentException("请求参数不能为空");
			}

			// 直接序列化原始对象（如 Map 或 POJO）
			String strParam = JSONObject.toJSONString(params);
			System.out.println("Sent JSON: " + strParam); // 调试输出

			StringEntity paramEntity = new StringEntity(strParam, "UTF-8");
			paramEntity.setContentType("application/json");
			post.setHeader("Authorization", token);
			post.setEntity(paramEntity);

			// 执行请求并处理响应
			HttpResponse httpResponse = trustAll().execute(post);
			org.apache.http.HttpEntity entity = httpResponse.getEntity();
			DistResult result = null;

			if (entity != null) {
				String responseString = EntityUtils.toString(entity, "UTF-8");
				Gson gson = new Gson();
				result = gson.fromJson(responseString, DistResult.class);
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DistResult.failure("请求失败", null);
	}


	public static DistResult doPostHttpsAirByToken(String host, Object params, String clientId, String secret) throws URISyntaxException {
		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectTimeout(3000)
				.setConnectionRequestTimeout(3000)
				.setSocketTimeout(3000)
				.build();

		HttpPost post = new HttpPost(host);
		post.setConfig(requestConfig);

		try {

			StringEntity paramEntity = new StringEntity(params.toString(), "UTF-8");
			paramEntity.setContentType("application/json");
			post.setHeader("x-client-id", clientId);
			post.setHeader("x-api-key", secret);
			post.setEntity(paramEntity);

			// 执行请求并处理响应
			HttpResponse httpResponse = trustAll().execute(post);
			org.apache.http.HttpEntity entity = httpResponse.getEntity();
			DistResult result = null;

			if (entity != null) {
				String responseString = EntityUtils.toString(entity, "UTF-8");
				Gson gson = new Gson();
				result = gson.fromJson(responseString, DistResult.class);
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DistResult.failure("请求失败", null);
	}






}
